package com.achilles.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.achilles.broadcast.AlarmReceiver;

import java.util.Calendar;

import static android.content.Context.ALARM_SERVICE;

/**
 * Created by AchillesL on 2016/10/1.
 */

public class AlarmUtils {

    public static final String KEY_ALARM_HOUR = "key_alarm_hour";
    public static final String KEY_ALARM_MINUTE = "key_alarm_minute";
    public static final String KEY_ALARM_ENABLE = "key_alarm_enable";

    public static void setAlarm(Context context, int hour, int minute) {
        Log.d("setAlarm", "hour: " + hour + " minute: " + minute);
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent
                .FLAG_UPDATE_CURRENT);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);

        Log.d("setAlarm", "calendar.getTimeInMillis(): " + calendar.getTimeInMillis());
        Log.d("setAlarm", "System.currentTimeMillis(): " + System.currentTimeMillis());

        if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }

        long timeDuration = calendar.getTimeInMillis() - System.currentTimeMillis();

        Log.d("setAlarm", "timeDuration: " + timeDuration);
        AlarmManager manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
                timeDuration, 24 * 60 * 60 * 1000, pendingIntent);

        saveAlarm2Local(context, hour, minute);
    }

    public static void cancelAlarm(Context context) {
        Log.d("cancelAlarm", "cancelAlarm");
        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent
                .FLAG_UPDATE_CURRENT);
        AlarmManager manager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
        manager.cancel(pendingIntent);
        SharedPreferencesUtils.saveBoolean(context, KEY_ALARM_ENABLE, false);
    }

    public static void saveAlarm2Local(Context context, int mRemainHour, int mRemainMinute) {
        SharedPreferencesUtils.saveBoolean(context, KEY_ALARM_ENABLE, true);
        SharedPreferencesUtils.saveInt(context, KEY_ALARM_HOUR, mRemainHour);
        SharedPreferencesUtils.saveInt(context, KEY_ALARM_MINUTE, mRemainMinute);
    }

    public static boolean isAlarmEnable(Context context) {
        return SharedPreferencesUtils.getBoolean(context, KEY_ALARM_ENABLE);
    }

    public static int getLocalAlarmHour(Context context) {
        return SharedPreferencesUtils.getInt(context, KEY_ALARM_HOUR);
    }

    public static int getLocalAlarmMinute(Context context) {
        return SharedPreferencesUtils.getInt(context, KEY_ALARM_MINUTE);
    }
}
